package com.lntea.mybatisDemo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.lntea.mybatisDemo.domain.TransRiskBean;
import com.lntea.mybatisDemo.mapper.TransRiskBeanMapper;

public class TransRiskBeanService {
	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void beforeQuery() {
		String resource = "mybatis-config.xml";
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	//查询拒付记录数
	public void queryRefuseCount(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setStartDate("2015-01-01");
			transRiskBean.setEndDate("2015-08-10");
			transRiskBean.setMerchantNo("Z08000000587759");
			Integer count = mapper.queryRefuseCount(transRiskBean);
			System.out.println(count);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询拒付记录列表
	public void queryRefuseList(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setStartDate("2015-01-01");
			transRiskBean.setEndDate("2015-08-10");
			transRiskBean.setMerchantNo("Z08000000587759");
			transRiskBean.setPage(1);
			transRiskBean.setRows(10);
			Integer count = mapper.queryRefuseCount(transRiskBean);
			if(count > 0){
				transRiskBean.getPager().setRowCount(count);
				List<TransRiskBean> list = mapper.queryRefuseList(transRiskBean);
				for(TransRiskBean temp : list){
					System.out.println(temp.getTransDateTime());
				}
			}
			System.out.println(count);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询拒付详细信息
	public void getRefuseDetail(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setMerchantNo("Z08000000587759");
			transRiskBean.setId(143467L);
			TransRiskBean result = mapper.getRefuseDetail(transRiskBean);
			if(result!=null){
				System.out.println(result.getOpContent()+":"+result.getCheckContent());
			}
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询风险交易数
	public void queryTransRiskCount(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setStartDate("2015-01-01");
			transRiskBean.setEndDate("2015-08-10");
			transRiskBean.setMerchantNo("Z07000000141863");
			Integer result = mapper.queryTransRiskCount(transRiskBean);
			System.out.println(result);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询风险交易列表
	public void queryTransRiskBeanList(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setStartDate("2015-01-01");
			transRiskBean.setEndDate("2015-08-10");
			transRiskBean.setMerchantNo("Z07000000141863");
			transRiskBean.setPage(1);
			transRiskBean.setRows(10);
			Integer count = mapper.queryTransRiskCount(transRiskBean);
			if(count > 0){
				transRiskBean.getPager().setRowCount(count);
				List<TransRiskBean> list = mapper.queryTransRiskList(transRiskBean);
				for(TransRiskBean temp : list){
					System.out.println(temp.getIsTimeout());
				}
			}
			System.out.println(count);
		} finally {
			session.close();
		}
	}
	
	@Test
	//查询风险交易详细信息
	public void getTransRiskDetail(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setMerchantNo("Z07000000141863");
			transRiskBean.setId(154475L);
			TransRiskBean result = mapper.getTransRiskDetail(transRiskBean);
			System.out.println(result.getOpContent()+":"+result.getCheckContent());
		} finally {
			session.close();
		}
	}
	
	@Test
	//根据条件查询所有风险交易的商户号
	public void queryRiskList(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setStartDate("2015-01-01");
			transRiskBean.setEndDate("2015-08-10");
			transRiskBean.setReplyStatus(0);
			/*List<TransRiskBean> list = mapper.queryRiskList(transRiskBean);
			for(TransRiskBean temp : list){
				System.out.println(temp.getMerchantNo());
			}*/
			
			List<String> list2 = mapper.queryRiskList2(transRiskBean);
			for(String temp : list2){
				System.out.println(temp);
			}
		} finally {
			session.close();
		}
	}
	
	@Test
	public void getTransRiskDetail2(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			TransRiskBeanMapper mapper = session.getMapper(TransRiskBeanMapper.class);
			TransRiskBean transRiskBean = new TransRiskBean();
			transRiskBean.setMerchantNo("Z08000000429253");
			transRiskBean.setId(168784L);
			TransRiskBean result = mapper.getTransRiskDetail2(transRiskBean);
			if(result != null){
				System.out.println(result.getFlowInfoList().get(0).getFilePath());
			}
		} finally {
			session.close();
		}
	}
}
